package com.roa.generics.connection;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * 
 * @author Jose Luis Rojas
 * 
 */
class DBConnection {

	private Connection connection;
	private static DBConnection dbconnection = null;

	public Connection getConnection() {
		return connection;
	}

	private void setConnection(Connection con) {
		this.connection = con;
	}

	private DBConnection() throws SQLException, NamingException {

		// Creo objeto del contexto inicial de ejecucion
		Context ctx = new InitialContext();
		
		DataSource ds = (DataSource)
		//ctx.lookup("java:jboss/SqlServer34/Gerft");
		ctx.lookup("jdbc/__SqlServer34");
		//Obtengo la conexion del pool y la asigno a la variable connection
		this.setConnection(ds.getConnection());


	}

	public static DBConnection connect() throws SQLException, NamingException {
		//Verifico que no exista ya una instancia de la conexion, si ya existe devuelvo la que ya existe, sino existe creo un solo objeto de conexion
		if (dbconnection == null) {
			dbconnection = new DBConnection();
		}
		return dbconnection;
	}

	public Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}

}
